package com.translate.web.service.impl;

import com.translate.web.config.contract.IOSSConfig;
import com.translate.web.constant.Constant;
import com.translate.web.dao.ManuscriptMapper;
import com.translate.web.param.contract.IIdAndUserIdParam;
import com.translate.web.param.contract.IManuscriptAddParam;
import com.translate.web.param.contract.ISecondaryServiceParam;
import com.translate.entity.Manuscript;
import com.translate.vo.contract.IManuscriptVO;
import com.translate.web.service.IManuscriptService;
import com.basic.service.impl.BaseServiceImpl;
import com.common.util.CopyEntity;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ManuscriptServiceImpl extends BaseTranslateServiceImpl<Manuscript> implements IManuscriptService {
	
	private IOSSConfig config;
	@Resource
	public void setConfig(IOSSConfig config) {
		this.config = config;
	}

	public ManuscriptServiceImpl(ManuscriptMapper mapper) {
		super(mapper);
	}

	protected void insertPropteris(Manuscript data) {
		long time = System.currentTimeMillis() / 1000;
		data.setCreateTime(time);
		data.setUpdateTime(time);
	}

	protected void updatePropteris(Manuscript data) {
		data.setUpdateTime(System.currentTimeMillis() / 1000);
	}

	protected Class<Manuscript> getEntityClass() {
		return Manuscript.class;
	}
	
	@Resource
	public void setRedisTemplate(RedisTemplate<String, ? extends Object> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	@Override
	public <VO> List<VO> getDatasByIdFromCache(IIdAndUserIdParam param, Class<VO> type, String key, int time) {
		// TODO Auto-generated method stub
		List<VO> datasByIdFromCache = super.getDatasByIdFromCache(param, type, key, time);
		
		if (0 == datasByIdFromCache.size()) {
			return new ArrayList<VO>();
		}
		
		String url = config.getURL();
		
		for (VO vo : datasByIdFromCache) {
			
			IManuscriptVO mo = (IManuscriptVO) vo;
			
			mo.setFile(url.concat(mo.getFile()));
			
		}
		
		return datasByIdFromCache;
	}

	/**
	 * 二次服务
	 */
	@Override
	@Transactional(propagation = Propagation.MANDATORY)
	public <M extends IManuscriptAddParam> int secondaryService(
			ISecondaryServiceParam<M> service, int payManagerId, int logId) {
		
		M[] param = service.getManuscripts();
		
		Manuscript[] manuscripts = new Manuscript[param.length];
		
		// 服务状态【0 初始 1 反馈 2 售后】
		// 0 正常 		10 售后  11 反馈 12 取消售后 13 取消 反馈
		Byte serviceStatus = service.getServiceStatus();
		
		serviceStatus = Constant.SECONDARY_T_SERVICE.get(serviceStatus);
		
		int orderId = service.getOrderId();
		
		long userId = service.getUserId();
		
		String url = config.getNoPerfixURL();
		
		for (int i = 0; i < param.length; i++) {
			
			param[i].setFile(param[i]
					.getFile()
					.replace("http://", "")
					.replace("https://", "")
					.replace(url, "")
			);
			
			manuscripts[i] = new  Manuscript();
			
			manuscripts[i].setPayManagerId(payManagerId);
			manuscripts[i].setOrderId(orderId);
			
			manuscripts[i].setSize(param[i].getSize());
			
			manuscripts[i].setFile(param[i].getFile());
			
			manuscripts[i].setOriginName(param[i].getOriginName());
			
			manuscripts[i].setOrderLogId(logId);
			
			manuscripts[i].setUserId((int) userId);
			
			manuscripts[i].setStatus(serviceStatus);
			
			this.insertPropteris(manuscripts[i]);
		}
		
		return this.batchInsert(manuscripts);
	}

	
	
	@Override
	protected Manuscript[] getUpdateData(int size) {
		// TODO Auto-generated method stub
		return new Manuscript[size];
	}

	@Override
	public int cancelSecondaryService(IIdAndUserIdParam userIdParam, 
			int logId, int status) {
		
		ManuscriptMapper mapper = this.getBaseMapper();
		
		return mapper.cancelSecondaryService(
				userIdParam.getId(), (int) userIdParam.getUserId(), logId, status);
	}
	
	
}